package com.czxy.Demo30;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws Exception {

        while (true) {
            System.out.println("1.账号登录 \n2.游客浏览");
            System.out.println("请输入序号:");
            Scanner sc = new Scanner(System.in);
            int i = sc.nextInt();
            if (i == 1) {
                boolean login = login();
                if (login == true){
                    return;
                }else{
                    continue;
                }
            }else if (i == 2){
                otherLogin();
                return;
            }else{
                System.out.println("请输入正确浏览方式！");
                continue;
            }
        }
    }

    public static void otherLogin() throws IOException {
        Date d = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("YYYY-MM-dd HH:mm");
        String sj = sim.format(d);
        FileUtils.writeStringToFile(new File("data30.txt") , "游客 "+sj+" 进入浏览！\n" , true);
    }

    public static boolean login() throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = sc.nextLine();
        System.out.println("请输入密码:");
        String pass = sc.nextLine();

        Properties pro = new Properties();
        FileInputStream fileInputStream = new FileInputStream("user.properties");
        pro.load(fileInputStream);
        fileInputStream.close();

        Date d = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("YYYY-MM-dd HH:mm");
        String sj = sim.format(d);

        for (Object o : pro.keySet()) {
            if (name.equals(String.valueOf(o)) && pass.equals(pro.getProperty(String.valueOf(o)))){
                FileUtils.writeStringToFile(new File("data30.txt") , name+" "+sj+" 登录成功\n" , true);
                return true;
            }
        }

        FileUtils.writeStringToFile(new File("data30.txt") , name+" "+sj+" 登录失败\n" , true);
        System.out.println("登陆失败！！！\n");
        return false;
    }
}
